package com.fruit.controller.app.category;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.constants.GoodsConstant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.common.Page;
import com.fruit.service.category.CategoryManager;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;


@Controller
@RequestMapping(value = "/app/")
public class CategoryController extends BaseController {

	@Resource(name="CategoryService")
	private CategoryManager categoryManager;
	
	/**
	 * 热门推荐
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/popular", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> popular(Page page,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "热门推荐开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//热门推荐
			Map<String, Object> popularMap = new HashMap<String, Object>();
			popularMap.put("start", (page.getPage()-1) * page.getShowNumber());
			popularMap.put("num", page.getShowNumber());
			popularMap.put("position", GoodsConstant.POPULAR);
			popularMap.put("curtime", DateUtil.getTime());
			List<Map<String, Object>> popularList = categoryManager.getPopularList(popularMap);//热门推荐
			if(popularList!=null){
				for(int i=0;i<popularList.size();i++){
					Map<String, Object> goodsMaps = popularList.get(i);
					String goodsNames = goodsMaps.get("levels") + "-" + goodsMaps.get("goodsName");
					goodsMaps.put("goodsName", goodsNames);
				}
			}
			resultMap.put("popularList",popularList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
	
	/**
	 * 根据分类id查询 此分类下的所有水果
	 * @param page
	 * @param id
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/category", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> category(Page page,Integer id,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "分类开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//分类
			Map<String, Object> classMap = new HashMap<String, Object>();
			classMap.put("start", (page.getPage()-1) * page.getShowNumber());
			classMap.put("num", page.getShowNumber());
			classMap.put("classId", id);
			classMap.put("curtime", DateUtil.getTime());
			List<Map<String, Object>> classList = categoryManager.getFruitByClassId(classMap);//分类
			if(classList!=null){
				for(int i=0;i<classList.size();i++){
					Map<String, Object> goodsMaps = classList.get(i);
					String goodsNames = goodsMaps.get("levels") + "-" + goodsMaps.get("goodsName");
					goodsMaps.put("goodsName", goodsNames);
				}
			}
			resultMap.put("classList",classList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
	
	@RequestMapping(value = "/getclassname", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getClassName(HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "分类开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//获取分类的名称
			List<Map<String, Object>> getClassNameList = categoryManager.getClassNameList();
			resultMap.put("getClassNameList",getClassNameList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
}
